<template>
  <div>
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="产品名称">
          <el-input v-model="formData.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品品牌">
          <el-input v-model="formData.brand" placeholder="请输入产品品牌" />
        </el-form-item>
        <el-form-item label="产品类别">
          <el-select v-model="formData.model" placeholder="请选择产品类别" clearable>
            <el-option
              v-for="item in modelclass"
              :key="item.id"
              :label="item.info"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>

    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="name" label="产品名字" />
        <el-table-column prop="brand" label="产品品牌" />
        <el-table-column prop="model.info" label="产品类别" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="最近更新" />
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

    <el-dialog
      title="添加商品种类"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <el-form ref="productInfo" :model="productInfo" label-suffix=":" label-position="left" :rules="rules">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="productInfo.name" placeholder="请输入产品名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="产品品牌" prop="brand">
          <el-input v-model="productInfo.brand" placeholder="请输入产品品牌" style="width: 300px" />
        </el-form-item>

        <el-form-item label="产品类型" prop="model">
          <el-select v-model="productInfo.model" placeholder="请选择产品类型" style="width: 300px">
            <el-option
              v-for="item in modelclass"
              :key="item.id"
              :label="item.info"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addproduct('productInfo')">添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import productApi from '@/api/product/productset'
export default {
  data() {
    return {
      formData: {

      },
      tableData: null,
      current: 1, // 当前页
      limit: 10, // 每页显示记录数
      total: 0, // 总记录数
      dialogVisible: false,
      productInfo: {
        name: '',
        brand: '',
        model: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        brand: [
          { required: true, message: '请输入产品品牌', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '请选择产品类型', trigger: 'change' }
        ]

      },
      modelclass: null
    }
  },
  created() {
    this.getdata()
    this.getclass()
  },
  methods: {
    getclass() {
      productApi.getclass().then(res => {
        this.modelclass = res.data
      })
    },
    remove(id) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        productApi.deleteproduct(id)
          .then(response => {
            // 提示
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            // 刷新页面
            this.getdata()
          })
        console.log(id)
      })
    },
    // 获取数据
    getdata() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      productApi.getAllProduct(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      productApi.getAllProduct(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    addproduct(productInfo) {
      // eslint-disable-next-line no-undef
      this.$refs[productInfo].validate((valid) => {
        if (valid) {
          productApi.addproduct(this.productInfo).then(res => {
            if (res.code === 500) {
              this.$message({
                type: 'warning',
                message: res.msg
              })
            }
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.dialogVisible = false
              this.productInfo = {}
              // 刷新页面
              this.getdata()
            }
          })
          // 添加商品
          // 添加商品
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped lang="scss">

</style>

